﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DeviceInfoManage.BusinessLogicLayer;

public partial class InspectManage : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (Session["adminUserName"] == null)
            {
                Response.Redirect("logout.aspx");
            }
            if (!string.IsNullOrEmpty(Request.QueryString["type"]))
            {
                hidType.Value = Request.QueryString["type"];
            }
            else
            {
                Response.Write("<script>alert('参数错误!');</script>");
            }
            if (hidType.Value == "2")
            {
                if (Session["adminType"].ToString() != "管理员")
                {
                    Btn_Add.Enabled = false;
                }
                BindData();
                Btn_Add.Text = "更新";
            }
        }
    }
    private void BindData()
    {
        if (!string.IsNullOrEmpty(Request.QueryString["id"]))
        {
            hidId.Value = Request.QueryString["id"];
        }
        InspectInfo ii = (new InspectInfo()).GetModel(int.Parse(hidId.Value));
        ddlDpt.SelectedValue = ii.Department;
        ddlPrincipal.SelectedValue = ii.Principal;
        txtDate.Text = ii.Date.ToString("yyyy-MM-dd");
        txtDetail.Text = ii.Detail;
        txtSuggestion.Text = ii.Suggestion;
        string[] items = ii.Item.Split(',');
        for (int i = 0; i < items.Length; i++)
        {
            SelectCheckBox(items[i]);
        }
    }

    private void SelectCheckBox(string value)
    {
        switch (value)
        {
            case "1": { cb1.Checked = true; break; }
            case "2": { cb2.Checked = true; break; }
            case "3": { cb3.Checked = true; break; }
            case "4": { cb4.Checked = true; break; }
            case "5": { cb5.Checked = true; break; }
            case "6": { cb6.Checked = true; break; }
        }
    }

    protected void Btn_Add_Click(object sender, EventArgs e)
    {
        /*建立一个记录对象，然后传入各个字段的属性值，最后调用插入函数执行添加*/
        InspectInfo ii = new InspectInfo();
        ii.Department = ddlDpt.SelectedValue;
        ii.Principal = ddlPrincipal.SelectedValue;
        ii.Date = DateTime.Parse(txtDate.Text);
        ii.Item = hidItem.Value;
        ii.Detail = txtDetail.Text;
        ii.Suggestion = txtSuggestion.Text;
        if (hidId.Value == "")
        {
            int i = ii.Add(ii);
            if (i != -1)
                Response.Write("<script>alert('添加成功!');location.href=location.href;</script>");
            else
                Response.Write("<script>alert('添加失败!');</script>");
        }
        else
        {
            ii.ID = int.Parse(hidId.Value);
            ii.Update(ii);
            Response.Write("<script>alert('更新成功!');location.href=location.href;</script>");
        }

    }
}
